<template>
	<view>
		<view class="padding-lr">
			<view class="text text-xl padding-top-sm text-bold">{{isMpBind?'绑定手机':'注册'}}</view>
			<view class="text-lg padding-tb-sm">您好，欢迎来到智税咨询</view>
		</view>
		<view class="padding margin flex bg-gray round align-center">
			
			<view class="padding-lr-sm solid-right">
				<view>
					+86<text class="icon-triangledownfill margin-left-xs"></text>
				</view>
			</view>
			<view class="padding-lr-sm"><input style="width: 100%;" v-model="mobile" type="number" placeholder="请输入手机号" class="text-df" /></view>
		</view>
		<view class="padding margin flex bg-gray round align-center">
			<view style="flex-grow: 1;" class="padding-lr-sm"><input type="number" v-model="code" style="width: 100%;" placeholder="请输入验证码" class="text-df" /></view>
			<view @click="getValidate" class="padding-lr-sm solid-left text-orange">{{show?"获取验证码":`已发送${count}s`}}</view>
		</view>
		<view class="padding margin flex bg-gray round align-center">
			<view class="padding-lr-sm"><input v-model="password" type="password" style="width: 100%;" placeholder="请输入登录密码" class="text-df" /></view>
		</view>
		<view class="padding margin flex bg-gray round align-center">
			<view class="padding-lr-sm"><input v-model="passwordd" type="password" style="width: 100%;" placeholder="请确认登录密码" class="text-df" /></view>
		</view>
		<!-- <view class="padding margin flex bg-gray round align-center">
			<view class="padding-lr-sm"><input v-model="parent_id" type="number" style="width: 100%;" placeholder="邀请码(选填)" class="text-df" /></view>
		</view> -->
		<view class="cu-bar justify-center padding-lr" style="width: 100%;">
			<button @click="submit" class="cu-btn bg-gradual-orange lg round" style="width: 100%; padding-top: 45upx;padding-bottom: 45upx;">{{isMpBind?'绑定手机':'注册'}}</button>
		</view>
		<view class="text-center padding-lr-sm padding-tb">
			<text @click="click1" class="text-orange margin-lr-xs">用户服务协议</text><text @click="click2" class="text-orange">隐私协议</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show:true,
				timer:null,
				count:'',
				mobile:'',
				code:'',
				password:'',
				passwordd:'',
				parent_id:'',
				key:'',
				config:{},
				isMpBind:false
			}
		},
		onLoad(op) {
			if(op.share_id){
				this.parent_id = op.share_id
			}
			// #ifdef MP
			if(op.bindMob){
				this.isMpBind = true
			}
			// #endif
			this.config = this.$store.state.config
		},
		methods: {
			getValidate(){
				if(!(/^1(3|4|5|7|8)\d{9}$/.test(this.mobile))){ 
					uni.showToast({
						title:"请输入正确的手机号码",
						icon:"none"
					})
					return 
				}
				const TIME_COUNT = 60;
				if (!this.timer) {
					this.count = TIME_COUNT;
					this.show = false;
					//此处写提交获取验证码的请求
					this.$request("/api/Member/sendSms","POST",{mobile:this.mobile}).then(res=>{
						if(res.status == 200){
							uni.showToast({
								title:res.msg,
								icon:"none"
							})
							this.timer = setInterval(() => {
								if (this.count > 0 && this.count <= TIME_COUNT) {
									this.count--;
								} else {
									this.show = true;
									clearInterval(this.timer);
									this.timer = null;
								}
							}, 1000)
							uni.setStorageSync('key',res.key)
							uni.setStorageSync('keyTime',new Date().getTime())
							this.key = res.key
						}else{
							uni.showToast({
								title:res.msg,
								icon:"none"
							})
							uni.removeStorageSync('key')
							uni.removeStorageSync('keyTime')
						}
					}).catch(err=>{
						this.$failToast()
					})
				}
			},
			submit(){
				if(!(this.mobile && this.password)){
					this.$failToast("请输入手机号和密码")
					return
				}
				if(this.password != this.passwordd){
					this.$failToast("密码不一致，请重新输入")
					return
				}
				if(!this.code){
					this.$failToast("请输入验证码")
					return
				}
				if(!this.key){
					this.$failToast("请获取验证码")
					return
				}
				
				let m_id = ''
				if(this.$store.state.userInfo.member_id){
					m_id = this.$store.state.userInfo.member_id
				}
				
				this.$request("/api/Member/add","POST",{
					key:this.key,
					code:this.code,
					mobile:this.mobile,
					passwd:this.password,
				}).then(res=>{
					uni.showModal({
						title:'提示',
						content:res.msg
					})
					if(res.status == 800){
						setTimeout(function() {
							uni.reLaunch({
								url: "/pages/login/login"
							})
						}, 500);
					}
				}).catch(err=>{
					this.$failToast()
				})
			},
			click1(){
				uni.navigateTo({
					url:"../content/content?content_id=1"
				})
			},
			click2(){
				uni.navigateTo({
					url:"../content/content?content_id=2"
				})
			}
		}
	}
</script>

<style>
	/*每个页面公共css */
	@import url("@/static/style/color-ui.css");
	page{
		background-color: #fff;
	}

</style>
